<?php
	require_once (dirname(__FILE__)."/../config/Config.inc");
	require_once (dirname(__FILE__)."/../config/api.inc");
	
	$parm = requestArrayParm($_REQUEST);
	
	$result['status'] = '1';
	
	if($parm['m'] == 'lucky2018_1')
	{
		if(empty($parm['openid']))
		{
			$result['status'] = '0';
			$result['msg'] = '缺少参数openid';
			echoJSON($result);
			exit;
		}
		unset($query);
		$query['openid'] = $parm['openid'];
		$wx_user = _SqlTool::getInfo('wx_users', $query);
		
		if(empty($wx_user))
		{
			$result['status'] = '0';
			$result['msg'] = '未查询到用户信息';
			echoJSON($result);
			exit;
		}
		
		unset($query);
		$query['openid'] = $parm['openid'];
		$query['source'] = $parm['m'];
		$wx_huodong = _SqlTool::getInfo('wx_huodong', $query);
		
		if(!empty($wx_huodong))
		{
			$result['status'] = '0';
			$result['msg'] = '已经参与过该活动';
			echoJSON($result);
			exit;
		}
		
		
		$a = array_fill(0,94,"1");
		$b = array_fill(95,1,"2");
		$c = array_fill(96,1,"3");
		$d = array_fill(97,1,"4");
		$e = array_fill(98,1,"5");
		$f = array_fill(99,1,"6");
		$arr = array_merge($a,$b,$c,$d,$e,$f);
		$key = array_rand($arr,1);
		
		unset($data);
		$data['openid'] = $parm['openid'];
		$data['source'] = $parm['m'];
		$data['award'] = $arr[$key];
		$data['addtime'] = time();
		$res = _SqlTool::addInfo('wx_huodong', $data);
		if(!empty($res))
		{
			$result['jiang'] = $arr[$key];
			$result['msg'] = '成功';
		}
		else
		{
			$result['status'] = '0';
			$result['msg'] = '失败';
		}

	}
	
	if($parm['m'] == 'lucky2018_2')
	{
		if(empty($parm['openid']))
		{
			$result['status'] = '0';
			$result['msg'] = '缺少参数openid';
			echoJSON($result);
			exit;
		}
		unset($query);
		$query['openid'] = $parm['openid'];
		$wx_user = _SqlTool::getInfo('wx_users', $query);
		
		if(empty($wx_user))
		{
			$result['status'] = '0';
			$result['msg'] = '未查询到用户信息';
			echoJSON($result);
			exit;
		}
		
		unset($query);
		$query['openid'] = $parm['openid'];
		$query['source'] = $parm['m'];
		$wx_huodong = _SqlTool::getInfo('wx_huodong', $query);
		
		if(!empty($wx_huodong))
		{
			$result['status'] = '0';
			$result['msg'] = '已经参与过该活动';
			echoJSON($result);
			exit;
		}
		
		$a = array_fill(0,94,"1");
		$b = array_fill(95,1,"2");
		$c = array_fill(96,1,"3");
		$d = array_fill(97,1,"4");
		$e = array_fill(98,1,"5");
		$f = array_fill(99,1,"6");
		$arr = array_merge($a,$b,$c,$d,$e,$f);
		shuffle($arr);
		$key = array_rand($arr,1);
		$jiang = $arr[$key];
		unset($data);
		$data['openid'] = $parm['openid'];
		$data['source'] = $parm['m'];
		$data['award'] = $jiang;
		$data['addtime'] = time();
		$res = _SqlTool::addInfo('wx_huodong', $data);
		if(!empty($res))
		{
			$result['jiang'] = $jiang;
			$result['msg'] = '成功';
		}
		else
		{
			$result['status'] = '0';
			$result['msg'] = '失败';
		}
	}
	
	if($parm['m'] == 'userinfo')
	{
		if(empty($parm['code']))
		{
			$result['status'] = '0';
			$result['msg'] = '缺少参数code';
			echoJSON($result);
			exit;
		}
		
		$access_token_arr = getOauthAccessCode($parm['code']);
		if(isset($access_token_arr['access_token']))
		{
			$access_token = $access_token_arr['access_token'];
			unset($query);
			$openid = $query['openid'] = $access_token_arr['openid'];
			$userinfo_arr = getOauthUserInfo($access_token, $openid);
			$info = _SqlTool::getInfo('wx_users',$query);
			if(!empty($info))
			{
				unset($query,$data);
				$query['id'] = $info['id'];
				$data['nickname'] = $userinfo_arr['nickname'];
				$data['headimgurl'] = $userinfo_arr['headimgurl'];
				$data['province'] = $userinfo_arr['province'];
				$data['city'] = $userinfo_arr['city'];
				$data['sex'] = $userinfo_arr['sex'];
				$res = _SqlTool::setInfo('wx_users',$query,$data);
				
			}
			else
			{
				unset($data);
				$data['openid'] = $userinfo_arr['openid'];
				$data['nickname'] = $userinfo_arr['nickname'];
				$data['headimgurl'] = $userinfo_arr['headimgurl'];
				$data['province'] = $userinfo_arr['province'];
				$data['city'] = $userinfo_arr['city'];
				$data['sex'] = $userinfo_arr['sex'];
				$data['addtime'] = time();
				$res = _SqlTool::addInfo('wx_users',$data);
			}
			
			if(!empty($res))
			{
				$result['userinfo'] = $userinfo_arr;
			}
			else
			{
				$result['status'] = '0';
				$result['msg'] = '获取微信信息失败';
				echoJSON($result);
				exit;
			}
		}
		else
		{
			$result['status'] = '0';
			$result['msg'] = '获取微信信息失败';
			echoJSON($result);
			exit;
		}
	}
	
	echoJSON($result);
	
	function getOauthAccessCode($code)
	{
		if(!empty($code))
		{
			$url = sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",'wx7a3ff5db84ef3504','8ae76ee667ee896d6efda9f769dee620',$code);
			$res_json = curl_send_post($url);
			$res = json_decode($res_json,true);
			if($res)
			{
				return $res;
			}
		}
		return false;
	}
	
	function getOauthUserInfo($access_token,$openid)
	{
		if(!empty($access_token) && !empty($openid))
		{
			$url = sprintf("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s",$access_token,$openid);
			$res_json = curl_send_post($url);
			$res = json_decode($res_json,true);
			if($res)
			{
				return $res;
			}
		}
		return false;
	}